<?
if ($_SESSION['sid']!=1) {
    exit('Access Denied!');
}

//跟1的区别是把总数的计算放在前面的循环里 后面的循环只负责向页面输出数据 试试效率
$atime = microtime(true);
ob_start(); 
include("/var/www/html/new/d/common/header.html");
include('/var/www/html/new/d/common/autoload.php');
$out_html = ob_get_contents();
ob_end_clean();
echo $out_html;
set_time_limit(0);
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$soap = SoapInit::GetSoapInside();

if(strtotime($_GET['endtime'])-strtotime($_GET['begintime'])>2764800){
                exit ("<script>
                            alert('因为订单量太多 所选时间间隔不能大于一个月');
                            history.go(-1);
                        </script>");  
                    
            }

$rew = array('result'=>array(),'totals'=>array());
if(!$_GET['begintime'] || !$_GET['endtime'])
	goto mark_one;
//处理请求的订单条件
$begintime=$_GET['begintime'].' 00:00:00';
$endtime=$_GET['endtime'].' 23:59:59';
switch ($_GET['ordertype']) {
	case '3':
        $otel = '12301';//导码的话 取票人手机都是12301
        break;
	
	default:
        $otel = '';
		break;
}
if($_GET['title']){
    $sql = "select id from uu_land where title like'%{$_GET['title']}%' limit 1";
    $GLOBALS['le']->query($sql);
    $GLOBALS['le']->fetch_assoc();
    $lid = $GLOBALS['le']->f('id');
}
goto mark_two;//此处打开 用本地查询  此处屏蔽 用s.12301.cc来查询
            
$url = "http://s.12301.cc/pft/uchat/other/orderReport.php";
$query =array(
    'begintime'=>$begintime,
    'endtime'=>$endtime,
    'csjhs'=>'7d9d6op56gdg1524h3b8',
    'otel'=>$otel,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
if($errno = curl_errno($ch)){
    print_r($errno);
    exit;
}
$rew = curl_exec($ch);
$rew = json_decode($rew,true);
// echo "<pre>";
// var_dump($rew);
// exit;
goto mark_one;
mark_two:
$i = 0;

  // 配置项
// @  每次循环查询的条数 在内存不溢出的情况下要尽可能的多 
// #目前的效率是77秒查询87000+个订单 每秒约1126

$n = 49999;

do {
unset($x);//unset能提升效率约10% 查2600+订单 没有unset时 需要2.2秒 有unset 需要2.0秒

//好像查询的时候效率会有10%的浮动...跟unset与否好像没什么关系
//晚上有空再来测试吧 周末是清明估计买票的人会很多就不测试了
$j = $i*$n;
$x= simplexml_load_string(
    $soap->Order_Globle_Search(
        "",'', $lid, "", "", "", "$begintime", "$endtime", "", "",
        "", "", "", "", "$otel", "", "", "", "", "",
        $j, $n, 0, 0, "", 0, "", 0, "", "",
        '', 0, 0, 0, ""),'SimpleXMLElement',LIBXML_COMPACT);
                                                                        
                                              

    foreach($x->Rec as $row){
        if($_GET['ordertype']=='2' && (int)$row->UUordertel==12301)
            continue;//查询散单时 过滤掉导码的
        $arrId = (int)$row->UUlid;
        $result[$arrId]['title'] = (string )$row->UUltitle;
        $result[$arrId]['order']++;        
        $result[$arrId]['tnum'] += (int)$row->UUtnum;
        $result[$arrId]['money'] += (int)$row->UUtnum * (int)$row->UUtprice;
        
        $totals['order'] ++;
		$totals['tnum'] += (int)$row->UUtnum;
        $totals['money'] += (int)$row->UUtnum * (int)$row->UUtprice;
        
        switch((int)$row->UUpmode){
                case 0:// 余额
                    $result[$arrId]['pmode0'] += (int)$row->UUtnum * (int)$row->UUtprice;
                    $result[$arrId]['pmode0_order'] ++;
                    $result[$arrId]['pmode0_tnum'] += (int)$row->UUtnum;
                    
                    $totals['pmode0']+= (int)$row->UUtnum * (int)$row->UUtprice;
                    $totals['pmode0_order'] ++;
                    $totals['pmode0_tnum'] += (int)$row->UUtnum;
                    
                    
                    break;
                case 1:// 支付宝
                    $result[$arrId]['pmode1'] += (int)$row->UUtnum * (int)$row->UUtprice;
                    $result[$arrId]['pmode1_order'] ++;
                    $result[$arrId]['pmode1_tnum'] +=(int)$row->UUtnum;
                    
                    $totals['pmode1']+= (int)$row->UUtnum * (int)$row->UUtprice;
                    $totals['pmode1_order'] ++;
                    $totals['pmode1_tnum'] += (int)$row->UUtnum;
                    
                    break;
                case 2:// 授信支付
                    $result[$arrId]['pmode2'] += (int)$row->UUtnum * (int)$row->UUtprice;
                    $result[$arrId]['pmode2_order'] ++;
                    $result[$arrId]['pmode2_tnum'] += (int)$row->UUtnum;
                    
                    $totals['pmode2']+= (int)$row->UUtnum * (int)$row->UUtprice;
                    $totals['pmode2_order'] ++;
                    $totals['pmode2_tnum'] += (int)$row->UUtnum;
                    
                    break;
                case 3: // 自销
                    $result[$arrId]['pmode3'] += (int)$row->UUtnum * (int)$row->UUtprice;
                    $result[$arrId]['pmode3_order'] ++;
                    $result[$arrId]['pmode3_tnum'] += (int)$row->UUtnum;
                    
                    $totals['pmode3']+= (int)$row->UUtnum * (int)$row->UUtprice;
                    $totals['pmode3_order'] ++;
                    $totals['pmode3_tnum'] += (int)$row->UUtnum;
                    
                    break;
                case 4:// 现场支付
                    $result[$arrId]['pmode4'] += (int)$row->UUtnum * (int)$row->UUtprice;
                    $result[$arrId]['pmode4_order'] ++;
                    $result[$arrId]['pmode4_tnum'] += (int)$row->UUtnum;
                    
                    $totals['pmode4']+= (int)$row->UUtnum * (int)$row->UUtprice;
                    $totals['pmode4_order'] ++;
                    $totals['pmode4_tnum'] += (int)$row->UUtnum;
                    
                    break;
                case 5:// 微信支付
                    $result[$arrId]['pmode5'] += (int)$row->UUtnum * (int)$row->UUtprice;
                    $result[$arrId]['pmode5_order'] ++;
                    $result[$arrId]['pmode5_tnum'] += (int)$row->UUtnum;
                    
                    $totals['pmode5']+= (int)$row->UUtnum * (int)$row->UUtprice;
                    $totals['pmode5_order'] ++;
                    $totals['pmode5_tnum'] += (int)$row->UUtnum;
                    
                    break;
                default:
                    break;
        }
        // print_r($result[$arrId]);
    }

++$i;
}while(count($x)==$n);
$rew=array('result'=>$result,'totals'=>$totals);




mark_one:

?>

<link rel="stylesheet" type="text/css" href="//www.12301.cc/shops/css/gmem.css"/>

<script language="javascript" src="//www.12301.cc/js/WdatePicker.js"></script>
<script src="../js/jsorder.js"></script>
<script type="text/javascript">  
 var tableToExcel = (function() {
  var uri = 'data:application/vnd.ms-excel;base64,'
    , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  return function(table, name) {
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
    window.location.href = uri + base64(format(template, ctx))
  }
})()
    </script>  
<link rel="stylesheet" type="text/css" href="//www.12301.cc/css/member.css"/>
<style>
	.report{overflow: hidden;margin:10px 0; padding:15px; border:1px solid #eee; }
	.report span.part{ margin-right:20px;}
	input.time{    background: url("../images/rili.png") no-repeat scroll right center #FFFFFF;
    border: 1px solid #7F9DB9;
    height: 20px;
    line-height: 20px;
    width: 120px;}
	.total{ width:100%; text-align:center;}
	input.checkbox{ vertical-align:-2px; margin-right:2px;}
	.btn{ background:#eee; padding:4px 15px; *padding:4px 8px; background:url("../images/mc_title.png") no-repeat right top; color:#fff; border:none; cursor:pointer}
	#excel{margin-left:10px;}
    .red {color:red}
</style>
<div class="mem_right">
    <form action="" method="get"  name="form" >
        <div class="mem_time">			
                <span class="lt">下单时间：</span>
                <input type="text" data-t="bt" id="txtStartTime" value="<?=$_GET['begintime']?>" name="begintime" data-msg="请选择时间段" class="textInp w175 alliNameInp" autocomplete="off"/> 至
                <input type="text" data-t="et" id="txtEndTime"  value="<?=$_GET['endtime']?>" name="endtime" data-msg="请选择时间段" class="textInp w175 alliNameInp" autocomplete="off"/>
                订单类型: <select name="ordertype">
                    <option value="1">全部</option>
                    <option value="2"
                        <?if($_GET['ordertype']=='2')
                            echo ' selected="selected"'?>
                        >散单</option>
                    <option value="3"
                        <?if($_GET['ordertype']=='3')
                            echo ' selected="selected"'?>
                        >导码</option>
                </select>
                景区名称:<input name="title" value="<?=$_GET['title']?>"/>
                <input type="submit" value="提交">
                <input type="button" onclick="tableToExcel('testTable', 'W3C Example Table')" value="导出到Excel">
            </div>
        </form>
    </div>
</div>
<div class="rank">
	<div class="rankcon">
		<table class="ranktab" id='testTable'>
			<thead id="rankhd">
				<th>序号</th>
				<th class="tL">景区名称</th>
				<th><span class="sort" onclick="sortTable('offmain',2, true ,this);">订单总数</span></th>
				<th><span class="sort" onclick="sortTable('offmain',3, true ,this);">门票总数</span></th>
				<th><span class="sort" onclick="sortTable('offmain',4, true ,this);">金额总数</span></th>
                
                <th><span class="sort" onclick="sortTable('offmain',5, true ,this);">订单数量</span></th>
				<th><span class="sort" onclick="sortTable('offmain',6, true ,this);">门票数量</span></th>
				<th><span class="sort red" onclick="sortTable('offmain',5, true ,this);">余额支付</span></th>
                
                <th><span class="sort" onclick="sortTable('offmain',7, true ,this);">订单数量</span></th>
				<th><span class="sort" onclick="sortTable('offmain',8, true ,this);">门票数量</span></th>
				<th><span class="sort red" onclick="sortTable('offmain',9, true ,this);">在线支付</span></th>
                
                <th><span class="sort" onclick="sortTable('offmain',10, true ,this);">订单数量</span></th>
				<th><span class="sort" onclick="sortTable('offmain',11, true ,this);">门票数量</span></th>
				<th><span class="sort red" onclick="sortTable('offmain',12, true ,this);">授信支付</span></th>
                
                <th><span class="sort" onclick="sortTable('offmain',13, true ,this);">订单数量</span></th>
				<th><span class="sort" onclick="sortTable('offmain',14, true ,this);">门票数量</span></th>
				<th><span class="sort red" onclick="sortTable('offmain',15, true ,this);">产品自销</span></th>
                
                <th><span class="sort" onclick="sortTable('offmain',16, true ,this);">订单数量</span></th>
				<th><span class="sort" onclick="sortTable('offmain',17, true ,this);">门票数量</span></th>
				<th><span class="sort red" onclick="sortTable('offmain',18, true ,this);">现场支付</span></th>
                
                <th><span class="sort" onclick="sortTable('offmain',19, true ,this);">订单数量</span></th>
				<th><span class="sort" onclick="sortTable('offmain',20, true ,this);">门票数量</span></th>
                <th><span class="sort red" onclick="sortTable('offmain',21, true ,this);">微信支付</span></th>                
                
			</thead>
            <tbody id="offmain">
				<?if(count($rew['result'])){
                foreach($rew['result'] as $lid=>$row){?>
                <tr data-id="">
					<td><?=++$m?></td>
					<td class="tL"><?=$row['title']?></td>
					<td><?=$row['order']?></td>
					<td><?=$row['tnum']?></td>
					<td><?=$row['money']/100?></td>
                    
                    <td><?=$row['pmode0_order'] ? $row['pmode0_order'] : 0?></td>
					<td><?=$row['pmode0_tnum'] ? $row['pmode0_tnum'] : 0?></td>
					<td><?=$row['pmode0']/100?></td>
                    
                    <td><?=$row['pmode1_order'] ? $row['pmode1_order'] : 0?></td>
					<td><?=$row['pmode1_tnum'] ? $row['pmode1_tnum'] : 0?></td>
					<td><?=$row['pmode1']/100?></td>
                    
                    <td><?=$row['pmode2_order'] ? $row['pmode2_order'] : 0?></td>
					<td><?=$row['pmode2_tnum'] ? $row['pmode2_tnum'] : 0?></td>
					<td><?=$row['pmode2']/100?></td>
                    
                    <td><?=$row['pmode3_order'] ? $row['pmode3_order'] : 0?></td>
					<td><?=$row['pmode3_tnum'] ? $row['pmode3_tnum'] : 0?></td>
					<td><?=$row['pmode3']/100?></td>
                    
                    <td><?=$row['pmode4_order'] ? $row['pmode4_order'] : 0?></td>
					<td><?=$row['pmode4_tnum'] ? $row['pmode4_tnum'] : 0?></td>
					<td><?=$row['pmode4']/100?></td>
                    
                    <td><?=$row['pmode5_order'] ? $row['pmode5_order'] : 0?></td>
					<td><?=$row['pmode5_tnum'] ? $row['pmode5_tnum'] : 0?></td>
					<td><?=$row['pmode5']/100?></td>
				</tr>
				<?}}?>
			</tbody>
			<tfoot>
            <?php if(count($rew['totals'])>0):?>
				<tr>
					<td  colspan="2">总计：</td>
					<td><?=$rew['totals']['order']?></td>
					<td><?=$rew['totals']['tnum']?></td>
					<td><?=$rew['totals']['money']/100?></td>                
                    
					<td><?=$rew['totals']['pmode0_order']?$rew['totals']['pmode0_order']:0?></td>
					<td><?=$rew['totals']['pmode0_tnum']?$rew['totals']['pmode0_tnum']:0?></td>
					<td><?=$rew['totals']['pmode0']?$rew['totals']['pmode0']/100:0?></td>

                    <td><?=$rew['totals']['pmode1_order']?$rew['totals']['pmode1_order']:0?></td>
                    <td><?=$rew['totals']['pmode1_tnum']?$rew['totals']['pmode1_tnum']:0?></td>
                    <td><?=$rew['totals']['pmode1']?$rew['totals']['pmode1']/100:0?></td>

                    <td><?=$rew['totals']['pmode2_order']?$rew['totals']['pmode2_order']:0?></td>
                    <td><?=$rew['totals']['pmode2_tnum']?$rew['totals']['pmode2_tnum']:0?></td>
                    <td><?=$rew['totals']['pmode2']?$rew['totals']['pmode2']/100:0?></td>

                    <td><?=$rew['totals']['pmode3_order']?$rew['totals']['pmode3_order']:0?></td>
                    <td><?=$rew['totals']['pmode3_tnum']?$rew['totals']['pmode3_tnum']:0?></td>
                    <td><?=$rew['totals']['pmode3']?$rew['totals']['pmode3']/100:0?></td>

                    <td><?=$rew['totals']['pmode4_order']?$rew['totals']['pmode4_order']:0?></td>
                    <td><?=$rew['totals']['pmode4_tnum']?$rew['totals']['pmode4_tnum']:0?></td>
                    <td><?=$rew['totals']['pmode4']?$rew['totals']['pmode4']/100:0?></td>
                    
					<td><?=$rew['totals']['pmode5_order'] ? $rew['totals']['pmode5_order'] : 0?></td>
					<td><?=$rew['totals']['pmode5_tnum'] ? $rew['totals']['pmode5_order'] : 0?></td>   
					<td><?=$rew['totals']['pmode5']/100?></td>
				</tr>
				</tr>
			<?php else:?>
				<tr>
					<td align="left" colspan="9">&nbsp;&nbsp;&nbsp;交易日期无销售订单！</td>
				</tr>
			<?php endif;?>
			</tfoot>
<?
//不写</table>是为了让下面计算的时间能显示在页面顶部
$mt = microtime(true) - $atime;
$rew['totals']['order'] = $rew['totals']['order'] ? $rew['totals']['order'] : 0;
echo "共".$rew['totals']['order']."张订单,    花费".$mt.'秒,    ';
echo $rew['totals']['order'] / $mt;
echo '张订单/每秒';
include '../common/footer.html';
?>
<script>
$("#txtStartTime").bind("click focus", function () {  
	
		   var endtimeTf = $dp.$('txtEndTime');             
		   WdatePicker({                            
			   maxDate: '#F{$dp.$D(\'txtEndTime\')}',      
			   dateFmt: "yyyy-MM-dd",              
			   onpicked: function () { endtimeTf.focus(); }          
				});         
		   });          
		   $("#txtEndTime").bind("click focus", function () {       
				WdatePicker({           
				   minDate: '#F{$dp.$D(\'txtStartTime\')}', 
				   dateFmt: "yyyy-MM-dd"       
			   });         
		   });
</script>

